<template>
  <hm-layout>
    <view class="publish-wrap">
      <view class="publish-wrap_inner">
        <el-form label-width="120px">
          <el-form-item label="名称" required>
            <el-input :maxlength="10" type="text" v-model="params.goods_name" :rows="5" class="form-input"
              placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="效果图" required>
            <uniImageUpload v-model="params.rendering_url[0]"></uniImageUpload>
            <uniImageUpload v-model="params.rendering_url[1]" class="ss-m-l-10"></uniImageUpload>
            <uniImageUpload v-model="params.rendering_url[2]" class="ss-m-l-10"></uniImageUpload>
            <uniImageUpload v-model="params.rendering_url[3]" class="ss-m-l-10" ></uniImageUpload>
            <uniImageUpload v-model="params.rendering_url[4]" class="ss-m-t-10"></uniImageUpload>
            <uniImageUpload v-model="params.rendering_url[5]" class="ss-m-l-10"></uniImageUpload>
            <uniImageUpload v-model="params.rendering_url[6]" class="ss-m-l-10"></uniImageUpload>
            <view class="tip">请上传3张效果图，1M以内</view>
          </el-form-item>
          <el-form-item label="姓氏个数" required>
            <el-input type="number" v-model="params.surname_nums" :rows="5" class="form-input"
              placeholder="请输入姓氏个数"></el-input>
            <view class="tip maring0">请如实填写，避免纠纷</view>
          </el-form-item>
          <el-form-item label="网盘分享链接" required>
            <el-input class="form-input" type="textarea" :rows="3" v-model="params.network_disk_path" placeholder="请输入网盘分享链接"></el-input>
            <view class="tip maring0">请如实填写，避免纠纷</view>
          </el-form-item>
          <el-form-item label="价格" required>
            <el-input type="number" v-model="params.diplayPrice" class="form-input" placeholder="请输入下载价格,单位元"></el-input>
          </el-form-item>
          <el-form-item label="排序">
            <el-input type="number" v-model="params.order" class="form-input" placeholder="排序"></el-input>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input type="textarea" v-model="params.keyword" :rows="5" class="form-input"
              placeholder="请输入关键字"></el-input>
            <view class="tip maring0">请如实填写，避免纠纷</view>
          </el-form-item>
          <el-form-item label="是否含底图">
            <el-switch v-model="params.contain_underlay" :active-value="1" :inactive-value="0"/>
          </el-form-item>
          <el-form-item label="是否原创">
            <el-switch v-model="params.is_original" :active-value="1" :inactive-value="0"/>
          </el-form-item>
          <el-form-item label="制作平台">
            <el-input class="form-input" v-model="params.platform" placeholder="请输入制作平台, 如意间 神采 文心一格 SD等"></el-input>
            <view class="tip maring0">请如实填写，避免纠纷</view>
          </el-form-item>
          
          <view class="submit-wrap">
            <el-button class="ui-BG-Main-Gradient sub-btn" type="primary" @click="onSubmit">发 布</el-button>
          </view>
        </el-form>
      </view>
    </view>
  </hm-layout>

</template>

<script setup>
  import {
    ref
  } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import uniImageUpload from '/common/components/UniImageUpload'
  const vk = uni.vk
  // do not use same name with ref
  let editId = ref(null)
  const params = ref({
    goods_name: '',
    surname_nums: null,
    rendering_url: [
    ],
    keyword: '',
    platform: '',
    network_disk_path: '',
    diplayPrice: null,
    price: null,
    order: 1
  })
  
  
  onLoad(options => {
    if (options._id) {
      
      editId.value = options._id
      vk.callFunction({
      	url: 'client/goods/kh/detail',
      	title: '请求中...',
      	data: {
      		_id: options._id
      	},
      	success: (res) => {
          
      		params.value = res.data
      	}
      });
    }
  })
  
 
  
  const onSubmit = async () => {
    console.log(params.value)
    let { underlay_url, rendering_url, keyword, platform, price, network_disk_path } = params.value
    let errorText = ''
    if ((!rendering_url[0] || !rendering_url[1] || !rendering_url[2]) && !errorText) errorText = '请上传效果图'
    // if (!keyword && !errorText) errorText = '请填写关键字'
    // if (!platform && !errorText) errorText = '请输入制作平台'
    if (!network_disk_path && !errorText) errorText = '请输入网盘路径'
    // if (!uni.vk.pubfn.test(price, 'money') && !errorText) errorText = '请填写正确金额'
    if (errorText) {
      return uni.vk.alert(errorText)
    }
    
    let data = {
      ...params.value,
      contain_keyword: params.value.keyword ? 1 :0,
      price: params.value.diplayPrice * 100,
      goodsClass: 1
    }
    if (editId.value) {
      params._id = editId.value
    }
    vk.callFunction({
    	url: 'client/goods/kh/publishAndUpdate',
    	title: '正在发布...',
    	data,
    	success: (data) => {
    		vk.toast(editId.value ? '修改成功' : '发布成功','none');
    	}
    });
  }
  const unpdateWatermarkUrl = (url) => {
    console.log(url)
    params.value.underlay_watermark_url = url
  }
  const handleInput = (value) => {
    console.log(value)
  }
</script>

<style lang="scss">
  .publish-wrap {
    padding: 30px;

    .publish-wrap_inner {
      width: 800px;
      margin: auto;
      background: #fff;
      border: 1px solid #e5e7eb;
      border-radius: 10px;
      padding: 20px 30px;

      .tip {
        margin-left: 30px;
        color: #999;

        &.maring0 {
          margin: 0;
        }
      }
      
      .submit-wrap {
        margin-top: 20px;
        text-align: center;
        
        .sub-btn {
          width: 300px;
          height: 45px;
          font-size: 16px;
        }
      }
    }
  }
</style>